package com.useeinfo.oa.open.api;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.useeinfo.framework.sugar.data.QueryParam;
import com.useeinfo.oa.common.utils.ResultUtil.ResultDto;
import com.useeinfo.oa.common.utils.ResultUtil.ResultUtil;
import com.useeinfo.oa.modules.base.biz.PositionBiz;
import com.useeinfo.oa.modules.base.entity.Enterprise;
import com.useeinfo.oa.modules.base.entity.Position;
import com.useeinfo.oa.modules.security.util.SessionKeyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/api/position/")
public class PositionAPI {
    @Autowired
    private PositionBiz positionBiz;

    /**
     * 获取组织列表
     */
    @RequestMapping(value = "/getPositionList")
    public ResultDto getAreaList(@RequestParam(value = "pageIndex", required = false) String pageIndexParam,
                                 @RequestParam(value = "pageSize", required = false) String pageSizeParam,
                                 HttpServletRequest request){
        Enterprise enterprise = SessionKeyUtils.getSessionEnterprise(request);
        QueryParam queryParam = new QueryParam(pageIndexParam, pageSizeParam);
        if(enterprise!=null){
            queryParam.getSqlMap().put("enterpriseId",enterprise.getId()+"");
        }
        List<Position> positionList = positionBiz.findList(queryParam);
        JSONArray array = new JSONArray();
        for (Position position : positionList){
            JSONObject json = new JSONObject();
            json.put("positionName",position.getName());
            json.put("positionCode",position.getCode());
            json.put("id",position.getId());
            array.add(json);
        }
        return ResultUtil.success(array);
    }
}
